<?php

class RankingModelDao {

	function RankingModelDao() {
	}

        function queryRankingByCompet($id_compet) {
		$ranking= array();
		$query  = "select T.LIB_TEAM LIKE 'freescale%' as FSL,"
				 ."T.LIB_TEAM, TC.ID_COMPETITION, G.ID_TEAM, IFNULL(SUM(G.POINTS), 0) AS POINTS, "
                 ."SUM(G.GOAL_F) AS GOAL_F, SUM(G.GOAL_D) AS GOAL_D, "
                 ."SUM(G.GOAL_F) - SUM(G.GOAL_D) AS DIFF, IFNULL(COUNT(G.NUM_GAMEPLAY), 0) AS NUM_GAMEPLAY, "
                 ."SUM(G.VICTORY) AS VICTORY, SUM(G.DRAW) AS DRAW, SUM(G.DEFEAT) AS DEFEAT "
                 ."FROM TBL_GAMEPLAY G "
                 ."INNER JOIN TBL_TEAM T ON T.ID_TEAM = G.ID_TEAM "
                 ."INNER JOIN  TEAM_HAS_COMPETITION TC ON T.ID_TEAM=TC.ID_TEAM "
                 ."INNER JOIN  TBL_MATCH M ON G.ID_MATCH=M.ID_MATCH "
                 ."WHERE TC.ID_COMPETITION=$id_compet AND M.ID_COMPETITION=$id_compet AND G.POINTS IS NOT NULL "
                 ."GROUP BY T.LIB_TEAM, TC.ID_COMPETITION, G.ID_TEAM "
                 ."ORDER BY POINTS DESC, DIFF DESC, NUM_GAMEPLAY ASC ";

		$result = mysql_query ($query) or trigger_error(mysql_error(), E_USER_ERROR);
		while ($row = mysql_fetch_assoc($result)) {
			$ranking[] = new RankingModel($row); 
		}
		return $ranking;
	}
}

?>
	